home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / spreotus / 123tech / macrolib.txt < prev    next >
Text File  |  1984-11-24  |  24KB  |  612 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.                                    1-2-3 Macro Tips
  10.                           A Collection of Illustrative Macros
  11.  
  12.           The Macro facility (The Typing Alternative) is a powerful tool; it can allow you
  13.           to get the most out of 1-2-3.  However, it can be a little intimidating.
  14.  
  15.           The basic concepts are simple.  When you use 1-2-3 without Macros, you press
  16.           keys to enter numbers and labels, to move the cell pointer, and to issue
  17.           commands.  Macros are labels containing just those keystrokes you would normally
  18.           type into 1-2-3.  There are some keys that you can't type directly into a label,
  19.           like the pointer movement keys and the function keys.  In Macros, these are
  20.           represented by words in braces, like {left} or {graph}.
  21.  
  22.           Of course, it can get more complicated.  There are special 1-2-3 commands, the
  23.           /X commands, that can only be used in Macros.  These determine which Macro
  24.           instructions are actually executed; they allow you to write Macros that are
  25.           computer programs.
  26.  
  27.           (If you haven't yet done so, read the 1-2-3 manual section on Macros before
  28.           going on.)
  29.           [23;1H[J
  30.           Key S or <ENTER> to continue[23;30H!s
  31.           [23;1H[JA good time to use a Macro is when you find yourself typing the same keystrok
  32.           again and again.  Just remember what keys you are striking, and type the same
  33.           strokes into a label, using the bracketed instructions when necessary.
  34.  
  35.           Macros are also problem-solvers.  One of the best times to write a Macro is when
  36.           you are trying to perform some task, and none of the 1-2-3 commands seems to do
  37.           exactly what you want.  Using a Macro, you can create your own commands!
  38.  
  39.           To help you get started, we've assembled some examples of Macros that we have
  40.           found useful.  These examples are presented in terms of problems and solutions,
  41.           because that's the way Macros tend to get developed.  However, even if you don't
  42.           think you will ever face such a problem, it's probably worth reading the example
  43.           anyway; they can teach you some useful general techniques.
  44.  
  45.           We've adopted a Macro-writing style that might be helpful.  The Macro name, and
  46.           other cells with range names in the Macro, are labeled in the column to the left
  47.           of the Macro.  This way, you can assign all the range names at one time, using
  48.           /Range Name Label Right.  Also, we capitalize range names within Macros, and use
  49.           lower case for the commands.  This makes the Macros easier to read.
  50.  
  51.           Here are some problems, and their Macro solutions.
  52.  
  53.                                     Editing Labels
  54.  
  55.           Problem:    To indent some labels.
  56.  
  57.                There are times when you've typed a column full of labels,
  58.                 and you want to indent some of them.  You could insert a
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                 column, and move over the labels to be indented, but this
  76.                 might be easier.
  77.  
  78.           Solution:   The indent (\I) Macro.
  79.  
  80.                Place the cell pointer on the label cell you want indented.
  81.                 press ALT-I.
  82.  
  83.             Version 1:
  84.  
  85.                  \I    {edit}   Go into Edit Mode.
  86.                        {home}   Put cursor at the beginning of Edit line.
  87.                        {right}  Move past the Label-Prefix.
  88.                          ~      Type two spaces and [Return] to leave Edit
  89.                                 Mode.
  90.  
  91.           This demonstrates the utility of using Macros to edit labels. With the {home},
  92.           {end}, {right} and {left} notations in your Macro, you can move around on the
  93.           edit line.  {del} and {bs} remove characters.  To insert characters, simply type
  94.           them into the Macro string.  If you want to leave Edit Mode include a tilde (~)
  95.           to represent an [Return].
  96.  
  97.           To use this Macro to indent a series of labels in a column, you must position
  98.           the cell pointer, press ALT-I, move the cell pointer down, press Alt-I, and so
  99.           on.  We can make the Macro more useful by adding the {down} key.
  100.  
  101.  
  102.                  \I    {edit}{home}    | This is Version 1.
  103.                        {right}  ~      |
  104.                        {down}            Move the cell pointer to the
  105.                                          next label.
  106.  
  107.           To use it, simply press ALT-I repeatedly.  You can manually skip over labels you
  108.           don't want to indent.
  109.  
  110.           Adding the {down} keystroke to the Macro makes it a lot easier to use.  Whenever
  111.           a Macro will be used repeatedly, on different cells in a particular order, it's
  112.           a good idea to include arrow keys at the end of the Macro, to move the cell
  113.           pointer to what will probably be the next cell.
  114.  
  115.           In fact, some useful Macros consist of just arrow keys.
  116.  
  117.                                     Movement Macros
  118.  
  119.           Problem: To move the cell pointer more than one cell at a time.
  120.  
  121.               You can create Macros that move the cell pointer in big
  122.                jumps.  These can be for special purposes, as when working on
  123.                data where corresponding numbers are six columns apart.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.               Or you can create a set of general purpose big step Macros.
  142.  
  143.           Solution: Big step Macros, Left (\L), Right (\R), Up (\U), Down
  144.                     (\D)
  145.  
  146.           You could also choose to give these Macros names of keys that form a diamond
  147.           pattern: Up (\I), Left (\J), Right (\K), and Down (\M).
  148.  
  149.                  \L {left}{left}{left}{left}
  150.  
  151.                  \R {right}{right}{right}{right}
  152.  
  153.                  \U {up}{up}{up}{up}
  154.  
  155.                  \D {down}{down}{down}{down}
  156.  
  157.           Now, to move in bigger steps, just press ALT-L, R, U or D.
  158.  
  159.           Here's one last motion-key Macro that lets you use the numeric keypad.
  160.  
  161.                                    Using the Keypad
  162.  
  163.           Problem:To use the numeric keypad without having to switch off
  164.                    the Num-Lock to move the cell pointer.
  165.  
  166.                   The problem with the numeric keypad is that serves two
  167.                    purposes: movement keys and numbers.  If you want to use
  168.                    the keypad as numbers, you can use the [Shift] key to
  169.                    temporarily turn them back into arrows.
  170.  
  171.                   This Macro, however, moves the cell pointer each time you
  172.                    enter a number, so you don't have to use any keys except
  173.                    the number and [Return] keys.
  174.  
  175.           Solution: The move Macro (\M).
  176.  
  177.           Use this when you want to enter a column or row of numbers using the keypad.
  178.           This example moves the cell pointer down after you press [Return].
  179.  
  180.           Use the {?} to make the Macro stop to get input before moving down.
  181.  
  182.                \M {?}     Wait for input; resume when [Return] is pressed.
  183.                   {down}  Move down.
  184.  
  185.           After you press ALT-M, 1-2-3 will wait until you press [Return] before
  186.           continuing with the execution of the Macro.  You can tell that you are in a
  187.           Macro by the little CMD indicator next to the Mode Indicator (upper right corner
  188.           of the screen).  And you can tell that it's waiting for input because the Mode
  189.           is READY.
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.           So far, this isn't very useful.  It will move the cell pointer down, but to use
  208.           it again, you must press ALT-M, which is about as much trouble as Shift-[Down
  209.           arrow].
  210.  
  211.           What we can do is make the Macro "loop," repeat itself.  To do this, use the /XG
  212.           command, which tells the Macro where to go to get its next instruction.  Note
  213.           that this is different from the {goto} function keystroke, which moves the cell
  214.           pointer, but does not affect the steps in the execution of the Macro.
  215.  
  216.           Place the cell pointer at the top of the column in which you want to type
  217.           numbers.  Press Alt-M and press the Num Lock key (or vice versa).  Type the
  218.           numbers you want to input, and press [Return] after each entry; the cell pointer
  219.           will move down automatically. When you are finished, press Ctrl-Break to stop
  220.           the Macro (and turn off the [Num Lock] so you can use the arrow keys).
  221.  
  222.           This Macro was discovered by Rich Landsman at Lotus.
  223.  
  224.                  \M    {?}       | This is version 1.
  225.                        {down}    |
  226.                        /xg\M~      Go back to the beginning of the Macro.
  227.  
  228.           This is an "infinite" loop; it keeps going around in circles until you stop it.
  229.           After you have finished putting your numbers in the column, press Ctrl-Break to
  230.           stop the Macro.
  231.  
  232.           Later we will see how to get a Macro loop to stop itself, but for now, let's
  233.           look at a different type of problem.
  234.  
  235.                                Putting a Value in a Cell
  236.  
  237.           Problem: To record the date of the last update.
  238.  
  239.                   Many people have found the @today function and the
  240.                    related Date formats to be a useful way of dating printed
  241.                    material.  However, when you use @today to put the date
  242.                    on a worksheet, and save it, the date will change when
  243.                    you read in the worksheet and recalculate it.  This is
  244.                    fine for some purposes, but doesn't give you a secure
  245.                    record of the last day a worksheet was revised.
  246.  
  247.           Solution: The Update (\U) Macro.
  248.  
  249.           Name the cell in which you want the date "DATE," and give it a date format.
  250.           After you have made your revisions, press ALT-U, and then save the file.
  251.  
  252.  
  253.             Version 1:
  254.  
  255.                  \U    {goto}DATE~  Go to the date cell.
  256.                        @today       Type in the function.
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.                        {calc}~      Replace the function with its present value.
  274.  
  275.           This works, but it has one problem.  It takes you from wherever you are on the
  276.           worksheet, and leaves you at "DATE."
  277.  
  278.             Version 2:
  279.  
  280.                  \U    /rncHERE~        Give the name "HERE"
  281.                        ~                 to the current cell pointer cell.
  282.                        {goto}DATE~    |
  283.                        @today         | This is Version 1.
  284.                        {calc}~        |
  285.                        {goto}HERE~      Return to your original position.
  286.                        /rndHERE~        Delete the name.
  287.  
  288.           Delete the name at the end of the Macro so that when you use it again, you don't
  289.           end up the last place it was created.  Remember: when you try to create a named
  290.           range for a name that already exists, the cell pointer goes back to the named
  291.           range's last position.
  292.  
  293.           You could insert a {bs} to bring it back to the position when the Macro was
  294.           invoked: /rncHERE~{bs}~, but DON'T.  If HERE's old position was referred to in a
  295.           formula (e.g.  +C5+HERE+E5), then after you moved it, the formula would include
  296.           HERE, meaning its new position.
  297.  
  298.           D5 the name HERE, the formula automatically became +C5+HERE+E5. And it would
  299.           stay that way, wherever you put HERE, until you /Range Name Delete HERE.
  300.  
  301.           Later on, we'll demonstrate Macros that depend heavily on range names, names
  302.           that are applied to first one cell, then another.  If you don't delete the range
  303.           name before applying the old range name to a new range, you can end up with all
  304.           your formulas and range names referring to the same cell.
  305.  
  306.           A good general rule is: whenever you create a range name within a Macro, delete
  307.           it before you exit, or before you change the location(s) to be referred to by
  308.           the name.
  309.  
  310.           Okay, one more version.  We don't really need it, but Version 2 does fail if
  311.           you've created another range named HERE.  Anyway, this is a chance to introduce
  312.           a nice technique (spelled "trick") that one of the Lotus staff discovered.
  313.  
  314.  
  315.                  \U    /dfDATE~@today~~~  Put the value of today's date in the
  316.                                            cell named "DATE."
  317.  
  318.           That's it!  For this gem, we can all thank Bill Liles, of Product Development
  319.           (this isn't the kind of development we expected, but don't get in the way of the
  320.           freight train of creativity).
  321.  
  322.           Bill found that you could use the /Data Fill command to plug values into cells.
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.           In fact, in the next example, we'll see how it can be used to increment,
  340.           decrement or otherwise operate on the value currently in a cell.
  341.  
  342.           Let's string that same Macro (Version 3) down a column so we can more easily
  343.           explain what is happening.
  344.  
  345.                  \U    /df       Execute the /Data Fill command.
  346.                        DATE~      on the range (one cell) named DATE.
  347.                        @today~   Make the Start value @today.
  348.                        ~~        Accept the defaults for Step and Stop
  349.                                   (they won't be used in a one-cell range).
  350.  
  351.           What is now in DATE is the VALUE of @today, not the function.
  352.  
  353.           The next example will show how to use this technique to count how many times you
  354.           loop, and stop you when you're done.  It also demonstrates how to use range
  355.           names to find out something about the current cell.
  356.  
  357.                                     Stopping a Loop
  358.  
  359.           Problem: To make some cells blank.
  360.  
  361.                   When 1-2-3 evaluates a formula it treats empty cells as
  362.                    having a value a value of zero.  This can create
  363.                    confusion when you have missing data.  You can substitute
  364.                    @NA for missing cells, but it doesn't look very neat.
  365.  
  366.                   Here is a listing of salaries for two years.  The user
  367.                    wanted to create a column with the percent change in
  368.                    salary, but some people weren't employed at this company
  369.                    for both years.  For them, he wanted just blank cells.
  370.  
  371.                             Salary 81   Salary 82
  372.                             $15,000     $16,500
  373.                                         $36,000
  374.                             $13,000     $14,500
  375.                             $35,000
  376.                             $19,500     $21,000
  377.  
  378.           Solution: A Macro and a formula.
  379.  
  380.           A standard formula for percent change in salary would be:
  381.  
  382.                    (Sal82-Sal81)/Sal81
  383.  
  384.           But this gives a value of @ERR when Salary 81 is missing, and a value of -1 when
  385.           Salary 82 is missing.  Let's embed our standard formula in an @if function
  386.           formula.
  387.  
  388.                   @IF((Sal81=0)#OR#(Sal82=0),999,(Sal82-Sal81)/Sal81)
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.           This takes on a value of 999 when either figure is missing; otherwise it gives
  407.           the correct value.  Here is how the figures look now.
  408.                             Salary 81   Salary 82  Percent change
  409.                             $15,000     $16,500         10.00%
  410.                                         $36,000      99900.00%
  411.                             $13,000     $14,500         11.54%
  412.                             $35,000                  99900.00%
  413.                             $19,500     $21,000          7.69%
  414.  
  415.           Version 1:
  416.  
  417.           Next, place the cell pointer at the top of the "Percent Change" column and press
  418.           ALT-B.  Repeat this until the entire column is cleaned up.
  419.  
  420.                  \B    /rncHERE~            Create the one-cell range, HERE.
  421.                        ~                     at the present position.
  422.                        /xi(HERE=999)~/re~   If HERE equals 999, erase the cell.
  423.                        /rndHERE~            Delete the cell name (!)
  424.                        {down}               And move down.
  425.  
  426.           And this is the result:
  427.  
  428.                             Salary 81   Salary 82  Percent change
  429.                             $15,000     $16,500         10.00%
  430.                                         $36,000
  431.                             $13,000     $14,500         11.54%
  432.                             $35,000
  433.                             $19,500     $21,000          7.69%
  434.  
  435.           Finally, we can put the above Macro in a loop that stops itself. To do this, we
  436.           will create ahead of time two one- cell named ranges, NCELLS and CNUMBER.
  437.           First, we'll count the number of cells in the column, and put the number in
  438.           NCELLS.  Then, each time we check (and perhaps erase) a cell, we'll add one to
  439.           CNUMBER.  When CNUMBER is greater than NCELLS, we're finished.
  440.  
  441.           Version 2:
  442.  
  443.                  \B    /rncHERE~             Create a range named HERE.
  444.                        {end}{down}~          Make it the whole column.
  445.                        /dfNCELLS~            Put in NCELLS the
  446.                        @count(HERE)~~~       number of cells in the column.
  447.                        /rndHERE~             Delete HERE.
  448.                        /dfCNUMBER~0~~~       Start counting cells with 0.
  449.                  LOOP  /rncHERE~           | ]<-- This cell is named "LOOP."
  450.                        ~                   |
  451.                        /xi(HERE=999)~/re~  | This is Version 1.
  452.                        /rndHERE~           |
  453.                        {down}              |
  454.                        /dfCNUMBER~           Increase the number in CNUMBER
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.                        CNUMBER+1~~~           by one (increment).
  472.                        /xi(CNUMBER<=NCELLS)~/xgLOOP~
  473.                                 This last line has the Macro resume
  474.                                  execution at the cell named LOOP, until the
  475.                                  number in CNUMBER is equal to the number in
  476.                                  NCELLS.
  477.  
  478.           One last example.  This one demonstrates how you can turn a number into a
  479.           formula, and back to a number, using {Edit}.
  480.  
  481.                                  Accumulating Numbers
  482.  
  483.           Problem: To Update a value.
  484.  
  485.                    The user was maintaining a database of sales people and
  486.                     their current monthly sales and sales year-to-date.  He
  487.                     wanted to be able to enter the monthly figures and
  488.                     update the year-to- date.
  489.  
  490.                          Salesperson       Year-to-date       Current
  491.                          DiAngelo               $83,000        $6,000
  492.                          Gottfried              $56,000        $8,000
  493.                          Jones                  $48,000        $5,000
  494.                          Washington             $77,000        $9,000
  495.                   These were the figures after the previous month's
  496.                    entries.  They are numbers, not formulas.
  497.  
  498.           Solution: The accumulate Macro (\A).
  499.  
  500.           This Macro takes advantage of the fact that while editing a formula, you can
  501.           point to cells, just as when you are first entering formulas.  You must be at
  502.           the end of the edit line and the last character must be one that could be
  503.           followed by a cell or range such as an operator (e.g.  +, -, #AND#) or an open
  504.           parenthesis.  To start pointing to cells, rather than move along the edit line,
  505.           press the {edit} key again to put you in VALUE mode.  When you then press the
  506.           pointer-movement key you will be in POINT mode, as when entering a formula.
  507.  
  508.           Place the cell pointer on the first "current" cell.  Press ALT-A. Type the new
  509.           current value and press [Return].  Press ALT-A again to enter the value for the
  510.           next salesperson.
  511.  
  512.           Version 1:
  513.  
  514.                  \A    /re~            Erase the old current value.
  515.                        {?}~            Wait for user to put in new value.
  516.                        {left}          Go to the year-to-date cell.
  517.                        {edit}          Edit it.
  518.                        +               Adding the plus turns it into a formula.
  519.                        {edit}          The {edit} key puts you in VALUE Mode.
  520.                        {right}~        Add the value in the "Current" cell.
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.                        {edit}{calc}~   Turn the formula back into a number.
  538.                        {right}{down}   Move to next current cell.
  539.           Remember that when the Macro is waiting for input ({?}) the CMD indicator is
  540.           next to the READY Mode indicator.
  541.  
  542.           Next, let's put the Macro in a loop, so that it will keep asking for this
  543.           month's figures, and stop when it reaches the end of the column.  The only
  544.           requirement is that there be no empty cells in the year-to-date column (except
  545.           at the end), so put zeros in any empty cells.
  546.  
  547.           This version uses a different technique to stop at the end of the column; it
  548.           checks to see if the next cell is blank, and if it is, it stops.
  549.  
  550.           Actually, there is a problem finding out if a single cell is blank.  Labels,
  551.           blank cells and zeros all have the value of zero, so you can't use that.  And
  552.           @count always has a value of one if its' argument is a one-cell range.
  553.  
  554.           This Macro uses @count and a two-cell range, named TEST.  The range consists of
  555.           the potential next "Current" cell and the one cell above it.  The value of
  556.           @count(TEST) will be 2, until it hits the end of the column, when it will be 1.
  557.  
  558.           Version 2:
  559.  
  560.                  \A    /re~            |
  561.                        {?}~            |
  562.                        {left}          |
  563.                        {edit}          |
  564.                        +               |  This is Version 1.
  565.                        {edit}          |
  566.                        {right}~        |
  567.                        {edit}{calc}~   |
  568.                        {right}{down}   |
  569.                        /rncTEST~{up}~
  570.                        /xi(@count(TEST)=2)~/rndTEST~/xg\A~
  571.                        /rndTEST~
  572.  
  573.           It's hard to put comments next to those long Macro lines, so let's take the last
  574.           three lines one at a time:
  575.  
  576.                       /rncTEST~{up}~
  577.  
  578.           Creates a two-cell range consisting of the potential next "Current" cell and the
  579.           one above it.
  580.  
  581.                       /xi(@count(TEST)=2)~/rndTEST~/xg\A~
  582.  
  583.           This checks to see if the value of @count(TEST) is still 2.  If it is, it
  584.           deletes TEST (important!) and tells the Macro to start again from the beginning.
  585.            This Macro label had to be long, because the /xi command tells the Macro to do
  586.           the rest of the Macro label, if the condition is true.
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.                       /rndTEST~
  605.  
  606.           And finally, to clean things up, we delete TEST at the end of the whole Macro.
  607.           We had to do this because when @count(TEST) is NOT 2, the rest of that line
  608.           doesn't get executed, and TEST still exists.
  609.  
  610.           Well, that should be enough to help you get started with Macros. Give them a
  611.           try, and write some of your own.
  612.